package com.wl.servlets;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet生命周期: 实例化、初始化、服务、销毁
 * 初始化有两个方法: init() init(ServletConfig config)
 *  获取config对象：ServletConfig config = getServletConfig()
 *      获取初始化参数值：config.getInitParameter(key)
 *      参数值可以在web.xml配置或者注解的方式配置
 *  ServletContext 可以在web.xml 配置 <context-param>
 *      可以通过ServletConfig获取, request获取, session获取
 *      初始值 context.getInitParameter(key)
 */
//@WebServlet(urlPatterns = {"/demo01"}, initParams = { @WebInitParam( name = "lei",value = "wang" )})
public class Demo01Servlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        String value = config.getInitParameter("wang");
        System.out.println("value = " + value);
        ServletContext context = config.getServletContext();
        String parameter = context.getInitParameter("view-suffix");
        System.out.println("parameter = " + parameter);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("demo01...");
        // 内部转发: 一次请求，服务器内部进行了多少次转发 客户端不知道
//        req.getRequestDispatcher("/demo02").forward(req,resp);

        // 重定向: 两次请求响应 地址栏有变化 客户端可看的url变化
        resp.sendRedirect("demo02");
    }
}
